home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus Special 16 / AMIGAplus Sonderheft 16 (1998)(ICP)(DE)[!].iso / pd / anwendungen / rtgmaster_dev / demos / flame / flame.c < prev    next >
C/C++ Source or Header  |  1998-01-22  |  8KB  |  324 lines

  1. /*
  2.         Flame
  3.  
  4.         A demo of rtg.library
  5.  
  6.         Written by Thomas & Hans-Jörg Frieden
  7.                    Schlossstr. 176
  8.                    54293 Trier
  9.                    tfrieden@fax.uni-trier.de
  10.                    hfrieden@fix.uni-trier.de
  11.  
  12. */
  13. //* "Includes"
  14. #include <stdlib.h>
  15. #include <proto/utility.h>
  16. #include <proto/exec.h>
  17. #include <clib/rtgmaster_protos.h>
  18. #include <clib/exec_protos.h>
  19. #include <clib/utility_protos.h>
  20. #include <pragmas/rtgmaster_pragmas.h>
  21. #include <pragmas/exec_pragmas.h>
  22. #include <pragmas/utility_pragmas.h>
  23. #include <exec/memory.h>
  24. #include <utility/tagitem.h>
  25. #include <rtgmaster/rtgmaster.h>
  26. #include <rtgmaster/rtgsublibs.h>
  27. #include <rtgmaster/rtgc2p.h>
  28. #include <proto/rtgmaster.h>
  29. #include "timer.h"
  30. #include <string.h>
  31. #include <stdio.h>
  32.  
  33. int currentbuffer=0;
  34.  
  35. //*
  36.  
  37. //* "Defines"
  38. #define XSIZE 80
  39. #define YSIZE 65
  40. #define YSCRN 60
  41. #define MSIZE (XSIZE*YSIZE)
  42. #define DECAY 3
  43. #define CBUF    76800
  44. //*
  45. //* "Pragmas"
  46. extern void __asm CopyFrame(register __a0 UBYTE *adr, 
  47.                             register __d0 ULONG size);
  48. extern void __asm CopyFrame2(register __a0 UBYTE *adr, 
  49.                              register __d0 ULONG aize);
  50. extern void __asm DrawMeter(register __a0 APTR buf, 
  51.                             register __d0 ULONG fps);
  52.  
  53. UBYTE MouseButton(void);
  54. //*
  55. //* "Globals"
  56. struct RtgScreen *RtgScreen;
  57. struct ScreenReq *sr;
  58. struct RTGMasterBase *RTGMasterBase;
  59. struct Library *UtilityBase;
  60. struct TagItem rtag[] = {
  61.     smr_MinWidth,       320,
  62.     smr_MinHeight,      200,
  63.     smr_MaxWidth,       1024,
  64.     smr_MaxHeight,      768,
  65.     smr_ChunkySupport,  512,
  66.     smr_PlanarSupport,  -1,
  67.     smr_Buffers,2,
  68.     smr_Workbench,1,
  69.     TAG_DONE,           NULL
  70. };
  71.  
  72. struct TagItem gtag[] = {
  73.     grd_BytesPerRow,    0,
  74.     grd_Width,          0,
  75.     grd_Height,         0,
  76.     grd_Depth,          0,
  77.     grd_PixelLayout,    0,
  78.     grd_ColorSpace,     0,
  79.     grd_PlaneSize,      0,
  80.     TAG_DONE,           0
  81. };
  82.  
  83. struct TagItem tacks[] = {
  84.     rtg_Buffers,2,
  85.     rtg_Workbench,0,
  86.     TAG_DONE,0
  87. };
  88.  
  89. UBYTE *sc1;
  90. UBYTE scrn[MSIZE];
  91. ULONG cmap[800];
  92. UBYTE blah;
  93. BOOL Planar;
  94. UBYTE *cbuf=NULL;
  95. ULONG width;
  96. ULONG height;
  97. UBYTE *sadr;
  98. ULONG num,avg;
  99. ULONG c2psignal;
  100. //*
  101. //* "NewFrame"
  102. void NewFrame(void) {
  103.     int i,x;
  104.     UBYTE *a;
  105.  
  106.     extern void GenFrame(void);
  107.  
  108.     a=&scrn[MSIZE-1]; i=XSIZE;
  109.  
  110.     while (i>0) {
  111.         x=rand();
  112.         if (x>RAND_MAX/2) *a=255;
  113.         else *a=0;
  114.         i--; a--;
  115.     }
  116.  
  117.     i=MSIZE-XSIZE-1;
  118.     a=scrn;
  119.  
  120.     GenFrame();
  121. }
  122. //*
  123. //* "DrawFrame"
  124. void DrawFrame(struct RtgScreen *s, ULONG size, ULONG ticks) {
  125.  
  126.     if (blah==0) CopyFrame(cbuf, size);
  127.     else CopyFrame2(cbuf,size);
  128.     DrawMeter(cbuf, ticks);  
  129.     CopyRtgBlit(RtgScreen,sadr,cbuf,0,0,0,width,height,width,height,0,0);
  130. }
  131. //*
  132. //* "DrawFrameP"
  133. void DrawFrameP(struct RtgScreen *s, ULONG size, ULONG ticks) {
  134.     UBYTE *adr;
  135.  
  136.     adr=cbuf;                               // Draw into chunky buffer first
  137.     if (blah==0) CopyFrame(adr, 320);
  138.     else CopyFrame2(adr,320);
  139.     DrawMeter(adr, ticks);
  140.     adr = sadr;
  141.     if (tacks[1].ti_Data==512) CopyRtgBlit(RtgScreen,sadr,cbuf,0,0,0,width,height,width,height,0,0);
  142.     // No Doublebuffering, if on Workbench Window
  143.     else
  144.     {
  145.      // For Kalms-c2p we have to use Doublebuffering
  146.      CopyRtgBlit(RtgScreen,GetBufAdr(RtgScreen,1-currentbuffer),cbuf,0,0,0,width,height,width,height,0,0);
  147.      SwitchScreens(RtgScreen,1-currentbuffer);
  148.      currentbuffer=1-currentbuffer;
  149.     }
  150. }
  151. //*
  152. //* "fail"
  153. void fail(void) {
  154.     if (RtgScreen) CloseRtgScreen(RtgScreen);
  155.     if (RTGMasterBase) CloseLibrary((struct Library *)RTGMasterBase);
  156.     if (UtilityBase) CloseLibrary(UtilityBase);
  157.     if (cbuf) FreeMem(cbuf, CBUF);
  158.     exit(0L);
  159. }
  160. //*
  161. //* "main"
  162. void main(int argc, char **argv) {
  163.    /*
  164.     * Since this is a demo, I don't check anything at all
  165.     * and simply assume that every open went ok... 8-)
  166.     */
  167.     int i, x;
  168.     struct TagItem *tag;
  169.     UBYTE rr, rg, rb;
  170.     ULONG size;
  171.     ULONG ticks=0, max=0, min=300;
  172.  
  173.     if (argc>1 && stricmp(argv[1],"small")==0) blah=1; else blah=0;
  174.         if (InitTimer()==FALSE) {
  175.         printf("Unable to open timer device\n");
  176.         fail();
  177.     }
  178.  
  179.     RTGMasterBase = (struct RTGMasterBase *)OpenLibrary((STRPTR)"rtgmaster.library", 0);
  180.     UtilityBase = OpenLibrary((STRPTR)"utility.library", 37L);
  181.     c2psignal=AllocSignal(-1);
  182.     sr = RtgScreenModeReq(rtag);
  183.     if (sr->Flags&sq_WORKBENCH) tacks[1].ti_Data=LUT8;
  184.     if (sr==NULL) fail();
  185.     if (c2psignal==-1) fail();
  186.     RtgScreen = OpenRtgScreen(sr, tacks);
  187.  
  188.     GetRtgScreenData(RtgScreen, gtag);
  189.  
  190.     tag=FindTagItem(grd_BytesPerRow, gtag);
  191.     size = tag->ti_Data;
  192.  
  193.     tag=FindTagItem(grd_Width, gtag);
  194.     width = tag->ti_Data;
  195.  
  196.     tag=FindTagItem(grd_Height,gtag);
  197.     height = tag->ti_Data;
  198.  
  199.     tag=FindTagItem(grd_PixelLayout, gtag);
  200.     if (tag->ti_Data != grd_PLANAR && tag->ti_Data != grd_CHUNKY) {
  201.         printf("Screenmode not supported\n");
  202.         fail();
  203.     }
  204.  
  205.     if (tag->ti_Data == grd_PLANAR) Planar = TRUE;
  206.     else Planar = FALSE;
  207.  
  208.     printf("Screen is %ld x %ld x %ld\n", gtag[1].ti_Data, gtag[2].ti_Data, gtag[3].ti_Data);
  209.     printf("It has %ld bytes per row", size);
  210.     if (size>gtag[1].ti_Data) {
  211.         printf(", which means that the screen is wider than what you see\n");
  212.     } else printf("\n");
  213.  
  214.         cbuf = AllocMem(CBUF, MEMF_CLEAR|MEMF_FAST);
  215.         if (cbuf==NULL) {
  216.             cbuf=AllocMem(CBUF, MEMF_CLEAR);
  217.             if (cbuf==NULL) {
  218.                 printf("Out of memory *SIGH*\n");
  219.                 fail();
  220.             }
  221.         }
  222.  
  223. if (gtag[3].ti_Data==8)
  224. {
  225.     cmap[0] = 256 * 65536;
  226.     rr = 0;
  227.     rg = 0;
  228.     rb = 0;
  229.     x = 1;
  230.     for (i = 0; i < 64; i++) {
  231.         cmap[x++] = rr * 0x1111111;
  232.         cmap[x++] = rg * 0x1111111;
  233.         cmap[x++] = rb * 0x1111111;
  234.         rr += 3;
  235.     }
  236.     for (i = 0; i < 127; i++) {
  237.         cmap[x++] = rr * 0x1111111;
  238.         cmap[x++] = rg * 0x1111111;
  239.         cmap[x++] = rb * 0x1111111;
  240.         rg += 3;
  241.     }
  242.     for (i = 0; i < 60; i++) {
  243.         cmap[x++] = rr * 0x1111111;
  244.         cmap[x++] = rg * 0x1111111;
  245.         cmap[x++] = rb * 0x1111111;
  246.         rb += 3;
  247.     }
  248.     for (i = 0; i < 4; i++) {
  249.         cmap[x++]=0xFFFFFFFF;
  250.         cmap[x++]=0xFFFFFFFF;
  251.         cmap[x++]=0xFFFFFFFF;
  252.     }
  253.     cmap[x++]=0;
  254. }
  255. else
  256. {
  257.    cmap[0] = 64 * 65536;
  258.     rr = 0;
  259.     rg = 0;
  260.     rb = 0;
  261.     x = 1;
  262.     for (i = 0; i < 16; i++) {
  263.         cmap[x++] = rr * 0x1111111;
  264.         cmap[x++] = rg * 0x1111111;
  265.         cmap[x++] = rb * 0x1111111;
  266.         rr += 12;
  267.     }
  268.     for (i = 0; i < 31; i++) {
  269.         cmap[x++] = rr * 0x1111111;
  270.         cmap[x++] = rg * 0x1111111;
  271.         cmap[x++] = rb * 0x1111111;
  272.         rg += 12;
  273.     }
  274.     for (i = 0; i < 12; i++) {
  275.         cmap[x++] = rr * 0x1111111;
  276.         cmap[x++] = rg * 0x1111111;
  277.         cmap[x++] = rb * 0x1111111;
  278.         rb += 12;
  279.     }
  280.     for (i = 0; i < 4; i++) {
  281.         cmap[x++]=0xFFFFFFFF;
  282.         cmap[x++]=0xFFFFFFFF;
  283.         cmap[x++]=0xFFFFFFFF;
  284.     }
  285.     cmap[x++]=0;
  286. }
  287.     LockRtgScreen(RtgScreen);
  288.     LoadRGBRtg(RtgScreen, (APTR) cmap);
  289.     UnlockRtgScreen(RtgScreen);
  290.  
  291.     if (RtgScreen) {
  292.         LockRtgScreen(RtgScreen);
  293.         sadr = (UBYTE *)GetBufAdr(RtgScreen,0);
  294.  
  295.         num=0;
  296.         while(MouseButton()==0) {
  297.             StartClock();
  298.             NewFrame();
  299.             if (Planar==FALSE) DrawFrame(RtgScreen, size, ticks);
  300.             else DrawFrameP(RtgScreen, size, ticks);
  301.             ticks=StopClock();
  302.             num++;
  303.             avg+=ticks;
  304.             if (max<ticks) max=ticks;
  305.             if (min>ticks) min=ticks;
  306.         }
  307.         UnlockRtgScreen(RtgScreen);
  308.         CloseRtgScreen(RtgScreen);
  309.     }
  310.     if (max==0) max=1;
  311.     if (min==0) min=1;
  312.     printf("Min Frame rate: %ld\n", max);
  313.     printf("Max Frame rate: %ld\n", min);
  314.     printf("Avg Frame rate: %ld\n", avg/num);
  315.     CloseTimer();
  316.     FreeSignal(c2psignal);
  317.     FreeMem(cbuf,CBUF);
  318.     FreeRtgScreenModeReq(sr);
  319.     CloseLibrary((struct Library *)RTGMasterBase);
  320.     CloseLibrary(UtilityBase);
  321. }
  322. //*
  323.  
  324.